package com.gy.flink.watermark;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import javax.annotation.Nullable;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;

/**
 * 每隔五秒计算最近10秒单词出现的次数
 */
public class TimeWindowWordCount2 {

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        env.setParallelism(1);

        //dufault 200ms产生一次水位时间
        env.getConfig().setAutoWatermarkInterval(1000);

        OutputTag<Tuple2<String, Long>> outputTag = new OutputTag<Tuple2<String, Long>>("last time"){};

        //步骤一：使用日志时间
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        DataStreamSource<String> dataStrean = env.addSource(editSource());

        SingleOutputStreamOperator<Tuple2<String, Integer>> result = dataStrean.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Long>> out) throws Exception {
                String[] split = value.split(",");
                for (String str : split) {
                    out.collect(Tuple2.of(str, Long.valueOf(split[1])));
                }
            }
        })
                //步骤二
                .assignTimestampsAndWatermarks(eventTimeExeractor())
                .keyBy(0)
                .timeWindow(Time.seconds(10), Time.seconds(5))
//                .allowedLateness(Time.seconds(5))//允许再延迟5秒 用处不大，直接配置水位旧可以了，反而每次窗口都会执行导致数据重复
                .sideOutputLateData(outputTag)//保留迟到太多的数据
//                .sum(1)
                .process(sumProcessWindow());

        result.print().setParallelism(1);

        //获取迟到太多的数据
        result.getSideOutput(outputTag).map(new MapFunction<Tuple2<String, Long>, String>() {
            @Override
            public String map(Tuple2<String, Long> value) throws Exception {
                return "迟到太多的数据：" + value.toString();
            }
        }).print().setParallelism(1);


        env.execute(TimeWindowWordCount2.class.getCanonicalName());

    }

    private static AssignerWithPeriodicWatermarks<Tuple2<String, Long>> eventTimeExeractor() {

        return new AssignerWithPeriodicWatermarks<Tuple2<String, Long>>() {
            private Long maxEnventTime = 0L;
            private final Long maxOutTime = 5000L;

            @Override
            public long extractTimestamp(Tuple2<String, Long> element, long previousElementTimestamp) {
                maxEnventTime = Math.max(maxEnventTime, element.f1);
                System.out.println(String.format("enventTime=%s, maxEnvenTime=%s, watermarkTime=%s",
                        LocalDateTime.ofInstant(Instant.ofEpochMilli(element.f1), ZoneId.systemDefault()),
                        LocalDateTime.ofInstant(Instant.ofEpochMilli(maxEnventTime), ZoneId.systemDefault()),
                        LocalDateTime.ofInstant(Instant.ofEpochMilli(getCurrentWatermark().getTimestamp()), ZoneId.systemDefault())));
                return element.f1;
            }

            @Nullable
            @Override
            public Watermark getCurrentWatermark() {
                //延迟水位
                return new Watermark(Instant.now().toEpochMilli() - maxOutTime);
            }
        };
    }


    private static ProcessWindowFunction<Tuple2<String, Long>, Tuple2<String, Integer>, Tuple, TimeWindow> sumProcessWindow() {
        /**
         * IN:输入数据类型
         * OUT:输出数据类型
         * KEY:分组的数据类型
         * W extends Window:窗口的类型
         */
        return new ProcessWindowFunction<Tuple2<String, Long>, Tuple2<String, Integer>, Tuple, TimeWindow>() {

            @Override
            public void process(Tuple tuple, Context context, Iterable<Tuple2<String, Long>> elements, Collector<Tuple2<String, Integer>> out) throws Exception {
//                System.out.println("当前系统时间：" + LocalDateTime.now());
//                System.out.println("window处理时间：" + LocalDateTime.ofInstant(Instant.ofEpochMilli(context.currentProcessingTime()), ZoneId.systemDefault()));
//                System.out.println("window开始时间：" + LocalDateTime.ofInstant(Instant.ofEpochMilli(context.window().getStart()), ZoneId.systemDefault()));
//                System.out.println("window结束时间：" + LocalDateTime.ofInstant(Instant.ofEpochMilli(context.window().getEnd()), ZoneId.systemDefault()));
                int count = 0;
                for (Tuple2<String, Long> ele : elements) {
                    count++;
                }
                out.collect(Tuple2.of(tuple.getField(0), count));
            }
        };

    }

    private static SourceFunction<String> editSource() {
        return new SourceFunction<String>() {

            /**
             * 控制10秒的倍数去发送数据
             * @param ctx
             * @throws Exception
             */
            @Override
            public void run(SourceContext<String> ctx) throws Exception {
                String currTime = String.valueOf(Instant.now().toEpochMilli());
                while (Integer.parseInt(currTime.substring(currTime.length() - 4)) > 100) {
                    currTime = String.valueOf(Instant.now().toEpochMilli());
                    continue;
                }

                System.out.println("开始发送数据的时间：" + LocalDateTime.now());
                //13秒发送一个
                TimeUnit.SECONDS.sleep(13);
                String envent = "hadoop," + Instant.now().toEpochMilli();
                ctx.collect(envent);

                //16秒发送一个
                TimeUnit.SECONDS.sleep(3);
                ctx.collect("hadoop," + Instant.now().toEpochMilli());

                //19秒发送一个（13秒产生的数据）  延迟的数据
                TimeUnit.SECONDS.sleep(3);
                ctx.collect(envent);

                //29秒发送一个（13秒产生的数据）  延迟太多的数据
                TimeUnit.SECONDS.sleep(10);
                ctx.collect(envent);

                TimeUnit.HOURS.sleep(2);

            }

            @Override
            public void cancel() {

            }
        };
    }

}
